﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint;

/*
Developed By: Niraj Kumar Rai
Developed On: 12-Aug-2011
Description: Delete the current site and download the content.
*/
namespace SiteProjects.Delete_Project
{
    public partial class Delete_ProjectUserControl : UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (isAuthor())
                imgDeleteProject.Enabled = true;
           
        }

        protected void imgDeleteProject_Click(object sender, ImageClickEventArgs e)
        {
            SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
            SPSiteCollection siteCollections = webApplication.Sites;

            try
            {
                //concnate the url with layouts folder
                string prpjectDelUrl = SPContext.Current.Site.RootWeb.Url + "/_layouts/DeleteProject/deleteProject.aspx";
                for (int i = 0; i < siteCollections.Count; i++)
                {
                    //Check site name is exist in collection   
                    if (SPContext.Current.Site.ServerRelativeUrl.ToUpper() == "/" + siteCollections.Names[i].ToUpper())
                    {
                        Response.Redirect(prpjectDelUrl, false);
                    }
                }
            }
            catch { }
        }

        private bool  isAuthor()
        {
            SPSite WebSite = SPContext.Current.Site;
            bool isAuthor = false; 

            string siteAuthor = string.Empty;
            string curUser = string.Empty;
            try
            {
                using (SPSite site = new SPSite(WebSite.Url))
                {
                    using (SPWeb web = site.OpenWeb())
                    {
                        siteAuthor = web.Author.ToString();
                        siteAuthor = siteAuthor.Replace("i:0#.w|", "");
                        siteAuthor = siteAuthor.Replace("i:0#.f|sqlmember|", "");

                        curUser = web.CurrentUser.ToString();
                        curUser = curUser.Replace("i:0#.w|", "");
                        curUser = curUser.Replace("i:0#.f|sqlmember|", "");

                        //if login user is site author then delete button will be visible.
                        if (siteAuthor == curUser)
                             isAuthor = true;
                    }
                }
            }
            catch { }

            return isAuthor;
        }

      
    }
}
